//Allen Ott-Character Movement, put everything together, also modified add and remove
//Chris Autwell-Story, Win Check, Helped on add and remove items
//Salwa- Random placement of items and the other stuff with items.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include<cstdlib>
#include "Dungeon.h"
#include "Item.h"
#include "location.h"
#include "character.h"
#include <ctime>

using namespace std;


int main()
{
    srand(time(NULL));
	int rows=0;     // room height
    int cols=0;		// room width
	char response; // for yes or no questions
	int direc;       //for the direction the user choose to go
	bool win = false; //to check for win
	//these are to keep up with were the items were placed.
    //int tempx1;
	//int tempy1;
	//int tempx2;
	//int tempy2;
	//int tempx3;
	//int tempy3;
	bool endroom = false;
	Item* item = NULL; //for pickup of item
		
	
	//Object Creation/Instantiation
	Character player;
    
try{
       Dungeon dungeon(rows, cols);
                
//Setup player and items before first display!!	

//setup character
dungeon.getRoom(player.getX(), player.getY()).setPlayer(& player); 

//setup items
	Item scepter;
	scepter.setName("Golden Scepter");
	scepter.setValue(10);
	Item scarab;
	scarab.setName("Emerald Scarab");
	scarab.setValue(5);
	Item ankh;
	ankh.setName("Ankh");
	ankh.setValue(2);

	//Place Items
	
	int itemX, itemY;
	for(int i = 0; i < 3; i++)
		{
			itemX = rand()%dungeon.getHeight();
			itemY = rand()%dungeon.getWidth();
			if(itemX == dungeon.getHeight()-1 && itemY == dungeon.getWidth()-1)
			{
				itemX = rand()%dungeon.getHeight();
				itemY = rand()%dungeon.getWidth();
			}
			if(dungeon.getRoom(itemX,itemY).getWall())
			{
				while(dungeon.getRoom(itemX,itemY).getWall())
				{
					itemX = rand()%dungeon.getHeight();
					itemY = rand()%dungeon.getWidth();
					if(itemX == dungeon.getHeight()-1 && itemY == dungeon.getWidth()-1)
					{
						//itemX = rand()%dungeon.getHeight();
						//itemY = rand()%dungeon.getWidth();
					}
				}
			}
			if(i==0)
            {	
				dungeon.getRoom(itemX,itemY).setItem(&scepter);
				//tempx1 = itemX;
				//tempy1 = itemY;
            }
    			else if(i==1)
    			{
    				dungeon.getRoom(itemX,itemY).setItem(&scarab);
    				//tempx2 = itemX;
    				//tempy2 = itemY;
                }
        			else
        			{
        				dungeon.getRoom(itemX,itemY).setItem(&ankh);
        				//tempx3= itemX;
        				//tempy3 = itemY;
                    }
		}
	
	//cout<<endl<<endl<< " The current position of the character is represented by the "<<(char)2<<"  in the dungeon. "<<endl;
	//cout<<" The items are represented by the  "<<(char)18<<" (Golden Scepter), the "<<endl<<endl;
	//cout<<(char)15<<" (Emerald Scarab), and the "<<(char)12<<" (Ankh) "<<endl<<endl;

//put the story and intro here and then clear the screen.   
    
    
//Main loop
 cout << "It is said that the pyramid of Egypts were built by the people of Egypt to bury" << endl;
 cout << "their Pharoah so he or she will ascend into the heavens above with the powerful" << endl; 
 cout << "Egyptian gods.  However, one Pharaoh was unwilling to cooperate with the gods'"<<endl; 
 cout << "way because he wanted ultimate power.  To try and overthrow the gods, he cast "<<endl; 
 cout << "an ancient curse on his sacred tomb.  The story goes that the one brave enough"<<endl;
 cout << "to claim the Pharoah's most cherished possesions would gain access to the power"<<endl; 
 cout << "of the Egyptian gods.  In turn, you will have ultimate power to do whatever you"<<endl;
 cout << "please. However, there are several difficult tasks ahead.  You must brave"<<endl; 
 cout << "through the Pharaoh's loyal servants, discover the Door of Destiny, and "<<endl;
 cout << "collect the Anhk("<<char(12)<<") of Anubis, the Emerald Scarab("<< char(15)<<") of Horus, and the Golden "<<endl; 
 cout << "Scepter("<<char(18)<<") of Amun."<<endl<<endl;
 cout << "For the one that accomplishes these tasks will face the Door of Destiny in" <<endl; 
 cout << "which the three items must be placed in order of the highest god to the lowest."<<endl; 
 cout << "Pick up the Anhk ("<<char(12)<<") first."<<endl;
 cout << "Pick up the Golden Scepter("<<char(18)<<") second."<<endl;
 cout << "Pick up the Emerald Scarab("<< char(15)<<") third."<<endl;
 cout << "Do you have what it takes to best the Egyptian gods?"<<endl;
 cout << "If so, then enter Relic Hunter..."<<endl<<endl;
system("pause");
system("cls");
while(win == false)
{   
    dungeon.display();
    cout << endl;
	cout << "BACKPACK" << endl;
	cout << "--------" << endl;
	for(int i = player.getNumItemsInKnapsack() - 1; i >= 0; i--) {
		cout <<i+1<<") "<< player.getKnap(i)->getName() << endl;
	
	}
	

    cout<<endl;
    cout<<"1. Move North"<<endl;
    cout<<"2. Move South"<<endl;
    cout<<"3. Move East"<<endl;
    cout<<"4. Move West"<<endl;
    cout<<"5. Drop an item in a room if you need to reoraganize your backpack"<<endl;
    cout<<"Please select which way you wish to move : ";
    cin>>direc;
    
    if(direc == 1)//this for when the player chooses to move north
    {        
             if (player.getX() <= 0 )
             { 
                   cout<<endl;
                   cout<<"Sorry you can't move through walls!!"<<endl;
                   cout<<"Please choose again"<<endl;
                   cout<<endl;
                   system("pause");
             }
                 else if((dungeon.getRoom(player.getX() - 1, player.getY()).getWall()) == true) 
                     
                  {
                     cout<<endl;
                     cout<<"Sorry you can't move through walls!!"<<endl;
                     cout<<"Please choose another way."<<endl;
                     cout<<endl;
                     system("pause");
                  }                   
                      else
                     {
                         dungeon.getRoom(player.getX(), player.getY()).setPlayer(NULL);
                         player.setX(player.getX() - 1);
                         dungeon.getRoom(player.getX(), player.getY()).setPlayer(& player);
                         
                     }
                     
     
               
             
    }            
    else if(direc == 2)//this is if the player chooses to move south.
    {
         if(player.getX() >= dungeon.getHeight()-1)
         {
            cout<<endl;
            cout<<"Sorry you can't move through walls!!"<<endl;
            cout<<"Please choose again"<<endl;
            cout<<endl;
            system("pause");
            
         }
             else if((dungeon.getRoom(player.getX() + 1 , player.getY()).getWall()) == true && player.getX() - 1 != dungeon.getHeight()  && player.getY() != dungeon.getWidth())
                  {
                    cout<<endl;
                    cout<<"Sorry you can't move through walls!!"<<endl;
                    cout<<"Please choose another way."<<endl;
                    cout<<endl;
                    system("pause");
                    
                  }                 
                     else
                     {
                         dungeon.getRoom(player.getX(), player.getY()).setPlayer(NULL);
                         player.setX(player.getX() + 1);
                         dungeon.getRoom(player.getX(), player.getY()).setPlayer(& player);
                         
                     }
         
         //item stuff goes here
                  
    }            
    else if(direc == 3)//this is for when the user choose to move right
    {
         if(player.getY() >= dungeon.getWidth()-1)
         {
            cout<<endl;
            cout<<"Sorry you can't move through walls!!"<<endl;
            cout<<"Please choose again"<<endl;
            cout<<endl;
            system("pause");
            
         }
             
             else if((dungeon.getRoom(player.getX(), player.getY() + 1).getWall()) == true && player.getX() - 1 != dungeon.getHeight()  && player.getY() != dungeon.getWidth())
              {
                 cout<<endl;
                 cout<<"Sorry you can't move through walls!!"<<endl;
                 cout<<"Please choose another way."<<endl;
                 cout<<endl;
                 system("pause");
                 
              }                 
                 else
                {
                     dungeon.getRoom(player.getX(), player.getY()).setPlayer(NULL);
                     player.setY(player.getY() + 1);
                     dungeon.getRoom(player.getX(), player.getY()).setPlayer(& player);
                 }
      //item stuff goes here
             
    }            
    else if(direc == 4)
    {
         if(player.getY() <= 0)
         {
            cout<<endl;
            cout<<"Sorry you can't move through walls!!"<<endl;
            cout<<"Please choose again"<<endl;
            cout<<endl;
            system("pause");
            
         }
             else if((dungeon.getRoom(player.getX(), player.getY() - 1).getWall()) == true)
              {
                 cout<<endl;
                 cout<<"Sorry you can't move through walls!!"<<endl;
                 cout<<"Please choose another way."<<endl;
                 cout<<endl;
                 system("pause");
                 
              }                 
                 else
                {
                     dungeon.getRoom(player.getX(), player.getY()).setPlayer(NULL);
                     player.setY(player.getY() - 1);
                     dungeon.getRoom(player.getX(), player.getY()).setPlayer(& player);
                 }
     
     
        
    }   
    else if(direc > 5 || direc < 0)
    {
        cout<<endl;
        cout<<"Not a valid choice. "<<"Please choose again."<<endl;
        cout<<endl;
        system("pause");
        
    }

    
    
    /*item stuff goes here///////////////////////////////////////////////////////*/ 
    system("cls");
    dungeon.display();

    
      cout<<endl;      
      if(direc != 5){
          if(dungeon.getRoom(player.getX(), player.getY()).getItem())
           {
              item = dungeon.getRoom(player.getX(), player.getY()).getItem();
           
            
                 if (item != NULL )
            		{
            			cout << "There is ";
            			if (item -> getName() == "Anhk" || item -> getName() == "Emerald Scarab")
            			{
            				cout << "an ";
            			}
                			else if( item -> getName() == "Golden Scepter")
                			{
                				cout << "a ";
                			}
            			
            			cout<<item -> getName()<<" in this room that is worth "<<item->getValue()<< " gold coins." << endl;
            			cout<< "Would you like to pick it up? "<<endl;
            			cout<<"Please enter y for yes or n for no: ";
            			cin>>response;
                        cout<<endl<<endl;
                        
                        
            			while (tolower(response) != 'y' && tolower(response) != 'n')
            			{
            				cout << "Please enter y for yes or n for no: ";
            				cin>>response;
            				cout<<endl<<endl;
            			}
            			if (toupper(response) == 'Y')
            			{
            				player.addToKnapsack(item);
            				cout << "Congratulations, you have obtained the " << item -> getName()<<"."<<endl;
                            cout<<"It was added to your backpack. ";
                            cout<<endl;
                            dungeon.getRoom(player.getX(), player.getY()).setItem(NULL);
                            cout<<endl;
                            system("pause");
            			}
            			else if(toupper(response) == 'N')
            			{
            				cout << "You just stare at the "<<item -> getName()<<" and decide its best to leave it alone." << endl;
            				cout<<endl;
                            system("pause");
            			}
            		} 
            		
            		
            } 
      }
//This is the remove from bag choice(i need to fix this)///////////////////////////////
    if(direc == 5)
    {
        if( dungeon.getRoom(player.getX(), player.getY()).getItem()){
            cout<<endl;
            cout<<"You can not drop two items in the same room."<<endl;
            cout<<"Please move to another location to drop the item."<<endl<<endl;
            system("pause");
        }
        else if(player.getX() == dungeon.getHeight()-1 && player.getY() == dungeon.getWidth()-1){
             cout<<" You can not drop the item in the room of the door of destiny"<<endl;
             cout<<"Go back one room and put the item their."<<endl<<endl;
             system("pause");
        }
        else
        {
             try 
             {             
                    item = player.removeFromKnapsack();
             
                    dungeon.getRoom(player.getX(), player.getY()).setItem(item);
                    
                    cout<<endl;         
                    cout<<"Item was removed succesfully."<<endl<<endl;
                    system("pause");                  
                     
             }
             catch(KnapsackEmptyException)
             {
                cout<<"Backpack is empty. "<<"Can not complete request."<<endl;
                system("pause");
             }
        }
    
    }       
//this is the check for win /////////////////////////////////////////////////////////////
    if(player.getX() == dungeon.getHeight()-1 && player.getY() == dungeon.getWidth()-1)
    {  
		if(player.getNumItemsInKnapsack() == 3) {
			if((player.getKnap(0)->getName() == "Ankh") && (player.getKnap(1)->getName() == "Golden Scepter") 
			   && (player.getKnap(2)->getName() == "Emerald Scarab")) {
	            win = true;
             }
            else if(player.getX() == dungeon.getHeight()-1 && player.getY() == dungeon.getWidth()-1){   
	                   
		         cout<<"You do not have right order of items to pass through the Door of Destiny"<<endl;  
		         cout<<"Please try to rearrange items."<<endl;
		         cout<<"To rearrange choose optoin 5 on the main menu."<<endl<<endl;
		         system("pause");
           }       
		}
		else if(player.getNumItemsInKnapsack() == 0){
             cout<<"Your backpack is empty"<<endl;
             cout<<"You must have all three items to try and complete the right combonation."<<endl;
             cout<<endl;
             system("pause");
        }
        else if(player.getNumItemsInKnapsack() == 1){
             cout<<"You only have 1 item in  your backpack."<<endl;
             cout<<"You must have all three items to try and complete the right combonation."<<endl;
             cout<<endl;
             system("pause");
        }
        else{ 
             cout<<"You only have "<<player.getNumItemsInKnapsack()<<" items."<<endl;
             cout<<"You must have all three items to try and complete the right combonation."<<endl;
             cout<<endl;
             system("pause");
        }
    }

    system("cls");
    	
          
}
         
dungeon.display();
if(win == true)
{
       cout<<"Congratulations you have made it to the Door of Destiny"<<endl<<"with the right order of the items!!"<<endl<<endl;
       cout<<"You win the game!!"<<endl;
       system("pause");
       system("cls");
       cout<<"The End"<<endl;
       
}
       
       
system("pause");
}
        
    catch (int){
          return 1;
          
    }

return 0;

}	








